package com.onec.service.queue;

import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentSkipListMap;

public class AddTask implements Runnable {


    private final ConcurrentLinkedDeque<String> queue;

    public AddTask(ConcurrentLinkedDeque<String> queue) {
        this.queue = queue;
    }


    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        for (int i = 0; i < 10000; i++) {
            queue.add(name + "ELEMENT:" + i);
        }
    }


    public static void main(String[] args) {
        ConcurrentLinkedDeque<String> concurrentLinkedDeque =
                new ConcurrentLinkedDeque<>();
        Thread[] threads = new Thread[100];

        for (int i = 0; i < threads.length; i++) {
            AddTask addTask = new AddTask(concurrentLinkedDeque);
            threads[i] = new Thread(addTask);
            threads[i].start();
        }
        System.out.println("-->" + concurrentLinkedDeque.size());
        for (int i = 0; i < threads.length; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-->" + threads.length);
        for (int i = 0; i < threads.length; i++) {
            ConcurrentSkipListMap concurrentSkipListMap = new ConcurrentSkipListMap();
            PollTask pollTask = new PollTask(concurrentLinkedDeque, concurrentSkipListMap);
            threads[i] = new Thread(pollTask);
            threads[i].start();
        }
        System.out.println("-->" + threads.length);
        for (int i = 0; i < threads.length; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-->" + concurrentLinkedDeque.size());
    }
}
